package com.stone.framework.message;

import java.util.List;

import org.springframework.data.domain.Page;

import com.baomidou.mybatisplus.core.metadata.IPage;

import io.swagger.annotations.ApiModelProperty;

/**
 * 分页请求报文
 * 
 * @author whocares
 * @date 2020/03/28
 */
public class PageResMessage<E> extends ResMessage<List<E>> {

    private static final long serialVersionUID = 6819070717505731609L;

    @ApiModelProperty(value = "總頁數", required = true)
    private Integer totalPages;

    @ApiModelProperty(value = "總記錄數", required = true)
    private Long totalRecords;

    @ApiModelProperty(value = "每頁記錄數", required = true)
    private Integer pageSize;

    /**
     * use for json
     */
    @Deprecated
    public PageResMessage() {}

    protected PageResMessage(List<E> body, Integer totalPages, Long totalRecords, Integer pageSize) {
        super(body, ResMessage.Header.SUCCESS);
        this.totalPages = totalPages;
        this.totalRecords = totalRecords;
        this.pageSize = pageSize;
    }

    public static <E> PageResMessage<E> success(Page<E> page) {
        PageResMessage<E> message =
            new PageResMessage<>(page.getContent(), page.getTotalPages(), page.getTotalElements(), page.getSize());
        return message;
    }

    public static <E> PageResMessage<E> success(IPage<E> page) {
        PageResMessage<E> message =
            new PageResMessage<>(page.getRecords(), (int)page.getPages(), page.getTotal(), (int)page.getSize());
        return message;
    }

    public Integer getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(Integer totalPages) {
        this.totalPages = totalPages;
    }

    public Long getTotalRecords() {
        return totalRecords;
    }

    public void setTotalRecords(Long totalRecords) {
        this.totalRecords = totalRecords;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    @Override
    public String toString() {
        return "PageResMessage [totalPages=" + totalPages + ", totalRecords=" + totalRecords + ", pageSize=" + pageSize
            + ", " + super.toString() + "]";
    }

}
